﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaterCloud.Code;
using WaterCloud.DataBase;
using WaterCloud.Domain.Entity.STZBManage;

namespace WaterCloud.Service.STZBManage
{
    /// <summary>
    /// 率土之滨收藏账号服务类
    /// </summary>
    public class Account_SC_Service : BaseService<Account_SC>, IDenpendency
    {
        public Account_SC_Service(ISqlSugarClient context) : base(context)
        {
        }

        /// <summary>
        /// 获取账号列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">关键词</param>
        /// <param name="sell_state">出售状态 1:公示期、2:可购买</param>
        /// <param name="state_val">账号状态</param>
        /// <param name="timeStr">公告日期范围</param>
        /// <param name="userid">当前登入用户id</param>
        /// <param name="monthNum">月数 默认最近一个月</param>
        /// <returns></returns>
        public async Task<List<Account_SC>> Get_Account_SC_GridJson(SoulPage<Account_SC> pagination, string keyword, int sell_state, int state_val, string timeStr, string userid, int monthNum)
        {
            DateTime stime = DateTime.Today;
            DateTime etime = stime.AddDays(1).AddHours(23).AddMinutes(59).AddSeconds(59);
            if (!string.IsNullOrEmpty(timeStr))
            {
                string[] times = timeStr.Split('-');
                stime = DateTime.Parse(times[0]);
                etime = DateTime.Parse(times[1]);
            }
            //获取数据
            var data = repository.Db_Context().Queryable<Account_SC>()
                .WhereIF(!string.IsNullOrEmpty(keyword), x => x.Cbg_Url.Contains(keyword))
                .WhereIF(sell_state > 0, x => x.Sell_state == sell_state)
                .WhereIF(state_val > 0, x => x.State == state_val)
                .WhereIF(!string.IsNullOrEmpty(timeStr), x => x.Gg_EndTime >= stime && x.Gg_EndTime <= etime)
                .Where(t => t.UserId == userid)
                .SplitTable(tak => tak.Take(monthNum));
            //权限过滤（保证分页参数存在）
            data = GetDataPrivilege("x", "", data);
            return await data.ToPageListAsync(pagination);
        }

        /// <summary>
        /// 删除收藏账号数据
        /// </summary>
        /// <param name="ids">账号数据id</param>
        /// <param name="userid">用户id</param>
        /// <returns></returns>
        public async Task Delete_Account(string[] ids, string userid)
        {
            //获取收藏账号数据
            var scList = await repository.Db_Context().Queryable<Account_SC>().Where(t => ids.Contains(t.F_Id) && t.UserId == userid).SplitTable().ToListAsync();
            //获取账号数据
            var list = await repository.Db_Context().Queryable<Account>().Where(t => ids.Contains(t.F_Id)).SplitTable().ToListAsync();
            //将账号数据收藏标识去除
            list.ForEach(t => t.Sc_Id = t.Sc_Id.Replace(userid + ",", ""));
            //开启事务
            await repository.Db_Context().Ado.BeginTranAsync();
            //删除收藏账号数据
            await repository.Db_Context().Deleteable<Account_SC>(scList).SplitTable().ExecuteCommandAsync();
            //修改账号数据
            await repository.Db_Context().Updateable<Account>(list).SplitTable().ExecuteCommandAsync();
            //提交事务
            await repository.Db_Context().Ado.CommitTranAsync();
        }
    }
}
